/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.alibaba.aone.casestudy.module.screen;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Action;
import com.alibaba.aone.casestudy.common.entity.ActionConditions;
import com.alibaba.aone.casestudy.service.ActionService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.dataresolver.Param;

/**
 * action screen类
 * 
 * @author gaojian 2011-4-25 上午10:15:39
 */
public class ActionList {

	@Autowired
	private ActionService actionService;

	public void execute(
			@Param("cgDepartment")String cgDepartment,
			@Param("chargeMan") String chargeMan,
			@Param("follower") String follower,
			@Param("department") String department,
			@Param("status") Integer[] statusArray,
			@Param("category") Integer[] categoryArray,
			@Param("latestCompleteTimeFrom") String latestCompleteTimeFromTemp,
			@Param("latestCompleteTimeTo") String latestCompleteTimeToTemp,
			@Param("realCompleteTimeFrom") String realCompleteTimeFromTemp,
			@Param("realCompleteTimeTo") String realCompleteTimeToTemp,
			@Param(name = "page", defaultValue = "1") Integer page,
			Context context) throws ParseException {
		ActionConditions actionConditions = new ActionConditions();
		List<Integer> statuses = new ArrayList<Integer>();
		statuses.addAll(Arrays.asList(statusArray));
		List<Integer> categories = new ArrayList<Integer>();
		categories.addAll(Arrays.asList(categoryArray));

		Date latestCompleteTimeFrom = null;
		Date latestCompleteTimeTo = null;
		Date realCompleteTimeFrom = null;
		Date realCompleteTimeTo = null;

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		System.out
				.println("<------------------------------------------------------->");
		System.out.println(chargeMan);
		System.out.println(follower);
		System.out.println(latestCompleteTimeFrom);
		System.out.println(latestCompleteTimeTo);
		System.out.println(realCompleteTimeFrom);
		System.out.println(realCompleteTimeTo);
		System.out
				.println("<------------------------------------------------------->");

		latestCompleteTimeFrom = StringUtils
				.isEmpty(latestCompleteTimeFromTemp) ? null : format
				.parse(latestCompleteTimeFromTemp);
		latestCompleteTimeTo = StringUtils.isEmpty(latestCompleteTimeToTemp) ? null
				: format.parse(latestCompleteTimeToTemp);
		realCompleteTimeFrom = StringUtils.isEmpty(realCompleteTimeFromTemp) ? null
				: format.parse(realCompleteTimeFromTemp);
		realCompleteTimeTo = StringUtils.isEmpty(realCompleteTimeToTemp) ? null
				: format.parse(realCompleteTimeToTemp);

		System.out
				.println("<------------------------------------------------------->");
		System.out.println(chargeMan);
		System.out.println(follower);
		System.out.println(latestCompleteTimeFrom);
		System.out.println(latestCompleteTimeTo);
		System.out.println(realCompleteTimeFrom);
		System.out.println(realCompleteTimeTo);
		System.out
				.println("<------------------------------------------------------->");

		actionConditions.setCgDepartment(cgDepartment);
		actionConditions.setChargeMan(chargeMan);
		actionConditions.setFollower(follower);
		actionConditions.setDepartment(department);
		actionConditions.setCategories(categories);
		actionConditions.setStatuses(statuses);
		actionConditions.setLatestCompleteTimeFrom(latestCompleteTimeFrom);
		actionConditions.setLatestCompleteTimeTo(latestCompleteTimeTo);
		actionConditions.setRealCompleteTimeFrom(realCompleteTimeFrom);
		actionConditions.setRealCompleteTimeTo(realCompleteTimeTo);
		actionConditions.setCurrentPage(page);

		List<Action> actions = actionService.search(actionConditions);

		context.put("actions", actions);
		context.put("query", actionConditions);

	}

	/*
	 * public void execute(@Param(name = "page", defaultValue = "1") Integer
	 * page, Context context) { ActionConditions conditions = new
	 * ActionConditions(); conditions.setCurrentPage(page); List<Action> actions
	 * = actionService.listActions(conditions); context.put("actions", actions);
	 * context.put("query", conditions); }
	 */
}
